using System;
using System.Drawing;

using SdlDotNet.Core;
using SdlDotNet.Input;

using Game.Input.GUI;
using Game.Output;

namespace Game.Input.Devices
{
	public static class CMouseHandler
	{
		public struct structMouseInfo
		{
			public bool		left;
			public bool		middle;
			public bool		right;
			
			public bool		leftAlreadyDown;
			public bool		middleAlreadyDown;
			public bool		rightAlreadyDown;
			
			public Point	position;
			public Point	lastPosition;
		}
		
		private static structMouseInfo	m_mouseInfo;
	
		public static void Initialize()
		{
			CConsole.ReportClassInit(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name);
			
			Mouse.ShowCursor = false; // disable windows cursor
			
			Events.MouseMotion += 
				new EventHandler<MouseMotionEventArgs>(ApplicationMouseMotionEventHandler);
			Events.MouseButtonDown += 
				new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
			Events.MouseButtonUp += 
				new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
		}
		
		// event handler, triggered by a click of any mouse button
		private static void ApplicationMouseButtonEventHandler(
			object sender,
			MouseButtonEventArgs args
		)
		{
			switch (args.Button)
			{
				case MouseButton.PrimaryButton:
					m_mouseInfo.left	= args.ButtonPressed;
					break;
					
				case MouseButton.SecondaryButton:
					m_mouseInfo.right	= args.ButtonPressed;
					break;
					
				case MouseButton.MiddleButton:
					m_mouseInfo.middle	= args.ButtonPressed;
					break;					
			}
		}
		
		// event handler, triggered when mouse is moved
		private static void ApplicationMouseMotionEventHandler(
			object sender,
			MouseMotionEventArgs args
		)
		{			
			m_mouseInfo.position = args.Position;
		}
		
		public static void Process()
		{
			if (m_mouseInfo.right)
			{
				CNavigation.Scroll(new Point(
					m_mouseInfo.position.X - m_mouseInfo.lastPosition.X,
					m_mouseInfo.position.Y - m_mouseInfo.lastPosition.Y
				));
			}	
			
			if (m_mouseInfo.left)
			{
//				CMenu.OnMouseDown();
				
				// mouse-button-click events
				if (!m_mouseInfo.leftAlreadyDown)
				{
					CConsole.ReportStatus("Left MB pressed");
					
					CCommands.Add(CCommands.CmdID.ADD_TILE, new Point(3,3), new Point(0,0), 1043);
					m_mouseInfo.leftAlreadyDown = true;
				}
				
				// mouse-button-hold-down events
				// ...
			}
			else
			{
				m_mouseInfo.leftAlreadyDown = false;				
			}
			
			m_mouseInfo.lastPosition = m_mouseInfo.position;
		}
		
		#region properties
		
		public static structMouseInfo MouseInfo
		{
			get {
				return m_mouseInfo;
			}
		}
		
		#endregion
	}
}
